package com.ruoyi.controller;

import cn.hutool.crypto.SecureUtil;
import cn.hutool.crypto.asymmetric.Sign;
import cn.hutool.crypto.asymmetric.SignAlgorithm;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.codec.Base64;

import javax.crypto.KeyGenerator;
import javax.crypto.Mac;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;

/**
 * @author Ericcc
 * @version v0.01
 * @date 2021/2/6
 */
public class Mytest extends Thread{


    public static void main(String[] args) throws NoSuchAlgorithmException, UnsupportedEncodingException {
        String url = "https://secure.payswiff.com/pgapp/integrations/pgmi/create-order";
        Map<String, String> map = new LinkedHashMap<>();
        map.put("merchantId", "1363370");
        map.put("merchantRefNo", "MRN123");
        map.put("apiVersion", "2.0.1");
        map.put("description", "123456");
        map.put("outletId", "0");
        map.put("currencyCode", "INR");
        map.put("locale", "EN-US");
        map.put("amount", "200");
        map.put("responseUrl", "https://www.google.com");
        map.put("customerMobileNumber", "9665272917");
        map.put("cardNumber", "1950104000023791");
        map.put("paymentMode", "1");

        String apiKey = "8C27A63577D3B67A9E9B9AD69B34C954";

        List<Map.Entry<String, String>> infoIds = new ArrayList<>(map.entrySet());
        // 对所有传入参数按照字段名的 ASCII 码从小到大排序（字典序）
        Collections.sort(infoIds, Comparator.comparing(Map.Entry::getKey));
        // 构造签名键值对的格式
        StringBuilder sb = new StringBuilder();
        sb.append(apiKey);
        for (Map.Entry<String, String> item : infoIds) {
            if (item.getKey() != null || item.getKey() != "") {

                String key = item.getKey();
                String val = item.getValue();
                if (!StringUtils.isNotBlank(val)) {
                    continue;
                } else {
//                    sb.append("|" + key + "_" + val);
                    sb.append("|" + val);
                }
            }
        }
        System.out.println(sb.toString());

        String password = sb.toString();

        MessageDigest md = MessageDigest.getInstance("SHA-512");
        md.update(password.getBytes());

        byte byteData[] = md.digest();

//Convert "byteData" to hex String:
        StringBuffer sb2 = new StringBuffer();
        for (int i = 0; i < byteData.length; i++) {
//            sb2.append(String.format("%02x", byteData[i]));
            sb2.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
        }

        System.out.println(sb2.toString());
        map.put("secureHash", sb2.toString());
/*        Sign sign = SecureUtil.sign(SignAlgorithm.SHA512withRSA);
        //签名str.getBytes("utf-8");
        byte[] signed = sign.sign(sb.toString().getBytes("utf-8"));
        map.put("secureHash", new String(java.util.Base64.getEncoder().encode(signed)));
        System.out.println(new String(java.util.Base64.getEncoder().encode(signed)));*/
        String s = JSON.toJSONString(map);
        System.out.println(s);
        String body = HttpRequest.post(url)
                .body(s)
                .execute().body();
        System.out.println(body);
/*        String result2 = HttpRequest.post("https://v2-api.bankopen.co/v1/payouts/otp/1")
                .header("Authorization","Bearer ad351de0-63dc-11eb-b1ef-2db3c8010ad4:53d0b2ec98b9e32666a22bf9b6ae87e6d56c8ebd")
                .body("{\"amount\":100,\"contact_number\":1234567891,\"email_id\":\"david830988@gmail.com\",\"currency\":\"INR\",\"mtx\":\"agaewga31508\"}")
                .execute().body();
        System.out.println(result2);*/
 /*       Mytest t1=new Mytest();
        t1.start();//线程的启动方法
        Mytest t2=new Mytest();
        t2.start();
        Mytest t3=new Mytest();
        t3.start();
        Mytest t4=new Mytest();
        t4.start();
        Mytest t5=new Mytest();
        t5.start();
        Mytest t6=new Mytest();
        t6.start();
        Mytest t7=new Mytest();
        t7.start();
        Mytest t8=new Mytest();
        t8.start();
        Mytest t9=new Mytest();
        t9.start();
        Mytest t10=new Mytest();
        t10.start();*/
    }

    private static String byte2Hex(byte[] bytes) {
        StringBuffer stringBuffer = new StringBuffer();
        String temp = null;
        for (int i = 0; i < bytes.length; i++) {
            temp = Integer.toHexString(bytes[i] & 0xFF);
            if (temp.length() == 1) {
                stringBuffer.append("0");
            }
            stringBuffer.append(temp);
        }
        return stringBuffer.toString();
    }


    @Override
    public void run() {
        String json = "{\n" +
                "    \"contains\":[\n" +
                "        \"payment\"\n" +
                "    ],\n" +
                "    \"account_id\":\"acc_FB4JBfqRFMFxTk\",\n" +
                "    \"payload\":{\n" +
                "        \"payment\":{\n" +
                "            \"entity\":{\n" +
                "                \"notes\":{\n" +
                "                    \"address\":\"Razorpay Corporate Office\"\n" +
                "                },\n" +
                "                \"fee\":24,\n" +
                "                \"created_at\":1611054619,\n" +
                "                \"amount_refunded\":0,\n" +
                "                \"acquirer_data\":{\n" +
                "                    \"rrn\":\"101939529656\"\n" +
                "                },\n" +
                "                \"captured\":true,\n" +
                "                \"contact\":\"+917879260843\",\n" +
                "                \"currency\":\"INR\",\n" +
                "                \"id\":\"1615193847609\",\n" +
                "                \"international\":false,\n" +
                "                \"email\":\"panikasinha3126@gmail.com\",\n" +
                "                \"amount\":1000,\n" +
                "                \"method\":\"upi\",\n" +
                "                \"vpa\":\"9739188251@paytm\",\n" +
                "                \"tax\":4,\n" +
                "                \"order_id\":\"11111111111112\",\n" +
                "                \"entity\":\"payment\",\n" +
                "                \"status\":\"captured\"\n" +
                "            }\n" +
                "        }\n" +
                "    },\n" +
                "    \"created_at\":1611055216,\n" +
                "    \"event\":\"payment.captured\",\n" +
                "    \"entity\":\"event\"\n" +
                "}";
        String result2 = HttpRequest.post("http://localhost/notify/razorpay")
                .body(json)
                .execute().body();
    }


    public void testTime(){
        /*        Date date = new Date();
        long d = date.getTime();
        d -= d%(5*60*1000);
        System.out.println(d);*/
        Calendar time = Calendar.getInstance();
        time.add(Calendar.DAY_OF_WEEK,0);
        time.set(Calendar.HOUR_OF_DAY, 0);
        time.set(Calendar.MINUTE, 0);
        time.set(Calendar.SECOND, 0);
        time.set(Calendar.MILLISECOND, 0);
        System.out.println(time.getTimeInMillis());

/*        Calendar beforeTime = Calendar.getInstance();
        beforeTime.add(Calendar.MILLISECOND, -5);
//        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        String mDateTime=formatter.format(beforeTime.getTime());
        System.out.println(beforeTime.getTime());*/
    }
}
